The Code

This is our programming code for Tennis the Menace. It is written in HBlogo.

 

to tennis-the-menace
thrash
forever [detect-ping-pong]
forever [scan]
end

to scan
go-straight-for 90
turn-left-for 40
go-straight-for 20
turn-left-for 40
go-straight-for 30
turn-left-for 40
go-straight-for 60
turn-left-for 40
go-straight-for 90
turn-left-for 57
go-straight-for 10
turn-left-for 57
go-straight-for 10
end

to go-straight-for :n
bc, onfor :n
end

to turn-left-for :n
b, onfor :n
end

to turn-left
left-wheel off
right-wheel on thisway
end

to turn-right
right-wheel off
left-wheel on thisway
end

to go-straight
left-wheel on thisway
right-wheel on thisway
end


to left-wheel
b,
end

to right-wheel
c,
end

global [x light]

to thrash
a, on
end

to detect-ping-pong
setx 0
loop [
wait-for-next-ball beep
setx x + 1
print x
]
end

to wait-for-next-ball
waituntil [not sees-ball?]
waituntil [sees-ball?]
end

to sees-ball?
output ((sensor 0) > 100)
end

to escape
go-straight
loop [ if (switch 1 ) [ bc, rd onfor 3 b, onfor random 5 c, onfor random 5] go-straight ]
end

to initialize
setlight 100 ; default threshold
left-wheel setpower 8
right-wheel setpower 8
;printblack
end